package com.evil.kingdom.pojo.entity.action;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Objects;

/**
 * 抽象坐标
 *
 * @author liyang
 * @date 2024-05-24 10:02
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AbstractPosition implements Position {

    /**
     * X坐标
     */
    @NotNull(message = "x坐标不能为空")
    private Double x;

    /**
     * y
     */
    @NotNull(message = "y坐标不能为空")
    private Double y;

    public AbstractPosition(Position position) {
        this.x = position.getX();
        this.y = position.getY();
    }

    public static AbstractPosition defaultPosition() {
        return new AbstractPosition(0d, 0d);
    }

    public boolean offset(double offset) {
        double rx = this.x + offset;
        double ry = this.y + offset;
        if (!this.checkPosition(rx) || this.checkPosition(ry)) {
            return false;
        }
        this.x = rx;
        this.y = ry;
        return true;
    }

    public void move(Position position) {
        this.x = position.getX();
        this.y = position.getY();
    }

    /**
     * 返回两点间距离
     *
     * @param position position
     * @return double
     */
    public double distance(Position position) {
        double px = position.getX() - this.getX();
        double py = position.getY() - this.getY();
        return Math.sqrt(px * px + py * py);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractPosition that = (AbstractPosition) o;

        if (!Objects.equals(x, that.x)) return false;
        return Objects.equals(y, that.y);
    }

    @Override
    public int hashCode() {
        int result = x != null ? x.hashCode() : 0;
        result = 31 * result + (y != null ? y.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return String.format("{x:%s, y:%s}", x, y);
    }

    public String simple() {
        return String.format("{%s-%s}", x, y);
    }
}
